//学生管理
layui.use(['form','jquery','table','layer','laydate'],function () {

    var form = layui.form;
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;
    var laydate = layui.laydate;


    //日期
    laydate.render({
        elem: '#birthday'
        ,type: 'datetime'
    });

    // 访问地址
    let basePath = "phoenix-springboot";
    let path =basePath+"/StudentfindAllormodil";

        //渲染表格组件(学生表)
        var tableIns = table.render({
            elem: "#studentTable",//绑定表格元素，推荐使用ID选择器
            toolbar: "#studentToolbar",
            url: "ExamfindAllormodil",//一步请求地址
            method: 'post',
            contentType: 'application/json',
            page: true,//开启分页
            // limit: 5,//设置每页显示条数,根据自己项目情况去设置
            // limits: [1, 2, 3, 4, 5, 6],//设置每页条数下拉框的选择项,就是分页那一栏的下拉框选项
            cols: [
                [
                    {
                        type: 'checkbox',
                        fixed: 'left',
                        width: 60
                    }, {
                    field: 'aname',
                    title: 'A名字',
                    fixed: 'left',
                    align: 'center',
                    width: 80,
                }, {
                    field: 'asex',
                    title: 'A性别',
                    align: 'center',
                    width: 80
                }, {
                    field: 'aage',
                    title: 'A年龄',
                    align: 'center',
                    width: 140
                },{
                    field: 'bname',
                    title: 'B名字',
                    fixed: 'left',
                    align: 'center',
                    width: 80,
                }, {
                    field: 'bsex',
                    title: 'B性别',
                    align: 'center',
                    width: 80
                }, {
                    field: 'bage',
                    title: 'B年龄',
                    align: 'center',
                    width: 140
                }
                    , {
                    field: 'ege',
                    title: 'B年龄',
                    align: 'center',
                    width: 140
                },{
                    fixed: 'right',
                    width: 248,
                    title: '操作',
                    align: 'center',
                    toolbar: '#barDemo'
                }
                ]
            ],
            done: function (res, curr, count) {
                // 性别
                $("[data-field='sex']").children().each(function () {
                    if ($(this).text() == 'M') {
                        $(this).text('男');
                    } else if ($(this).text() == 'F') {
                        $(this).text('女');
                    }
                });
   $("[data-field='ege']").children().each(function () {
                    if ($(this).text() >= 20) {
                        $(this).text('父子母子');
                    }if ($(this).text() > 60) {
                       $(this).text('祖孙关系');
                   }
       else if ($(this).text() < 'F') {
                        $(this).text('兄弟姐妹');
                    }
                });

                $("[data-field='idType']").children().each(function () {
                    if ($(this).text() == '01') {
                        $(this).text('身份证');
                    } else if ($(this).text() == '02') {
                        $(this).text('军人证');
                    } else if ($(this).text() == '03') {
                        $(this).text('护照');
                    } else if ($(this).text() == '04') {
                        $(this).text('户口 本');
                    } else if ($(this).text() == '05') {
                        $(this).text('外国人永久居留证');
                    } else if ($(this).text() == '06') {
                        $(this).text('武警证');
                    } else if ($(this).text() == '07') {
                        $(this).text('公章');
                    } else if ($(this).text() == '08') {
                        $(this).text('工商营业执照');
                    } else if ($(this).text() == '09') {
                        $(this).text('法人代表证');
                    } else if ($(this).text() == '10') {
                        $(this).text('学生证');
                    } else if ($(this).text() == '11') {
                        $(this).text('士兵证');
                    } else if ($(this).text() == '16') {
                        $(this).text('港澳居民来往内地通行证');
                    } else if ($(this).text() == '17') {
                        $(this).text('台湾居民来往大陆通行证');
                    } else if ($(this).text() == '18') {
                        $(this).text('其他证件类型');
                    }
                });

                // $("[data-field='birthday']").children().each(function () {
                //     var birthday = $(this).text();
                //     if (birthday.indexOf('出生日期') == -1) {
                //         $(this).text(getYMDHMS(parseInt(birthday)));
                //     }
                // });
                //
                // $("[data-field='createDate']").children().each(function () {
                //     var birthday = $(this).text();
                //     if (birthday.indexOf('创建时间') == -1) {
                //         $(this).text(getYMDHMS(parseInt(birthday)));
                //     }
                // });
                //
                // $("[data-field='updateDate']").children().each(function () {
                //     var birthday = $(this).text();
                //     if (birthday.indexOf('修改时间') == -1) {
                //         $(this).text(getYMDHMS(parseInt(birthday)));
                //     }
                // });

            }


        });

        //监听(模糊查询)搜索按钮提交事件
        form.on("submit(doSearch)", function (data) {
            // alert(data);

            tableIns.reload({
                where: data.field,//查询条件
                page: {
                    curr: 1
                }
            });


            //ajax请求
            // $.get("",{},function (result) {
            //
            // }."json");


            //禁止页面刷新
            return false;
        });

    //监听(模糊查询)搜索按钮提交事件
    form.on("submit(export)", function (data) {
        // alert(data);
        // layui.layer.confirm('is not?', function(index){
//
        layer.confirm("确定导出吗?",function (index) {
//         location.href="export";
//             $.post("export",data.field,function (result) {
//
//             });

            $.ajax({
                type:"post",
                url:"export",
                contentType: "application/json;charset=utf-8",
                data:JSON.stringify(data.field),
                dataType:"json",
                // success:function () {
                //     layer.alert("导出成功", {icon: 1});
                // },error:function () {
                //     layer.alert("导出失败", {icon: 2});
                // }
            });
            layer.alert("导出成功", {icon: 1});

            layui.layer.close(index);


    })
        tableIns.reload({
            where: data.field,//查询条件
            page: {
                curr: 1
            }
        });


        //ajax请求
        // $.get("",{},function (result) {
        //
        // }."json");


        //禁止页面刷新
        return false;
    });


        //监听格头部工具栏事件(学生表的)
        table.on("toolbar(studentTable)", function (obj) {

            switch (obj.event) {
                //添加
                case 'add':
                    openAddwindow();
                    break;

                //批量删除
                case 'batchDelete':
                    batchDelete();
                    break;



            }
        });
        var mainIndex;//窗口索引
        var urll;   //窗口地址

        //打开添加窗口
        function openAddwindow() {
            mainIndex = layer.open({
                type: 1,
                title: "添加账单",
                area: ['800px', '600px'],
                maxmin: true,
                content: $("#addOrUpdateBillDiv"),//应用的窗口代码
                success: function () {
                    //清空表单数据
                    $("#dataFrm1")[0].reset();//JavaScript中的方法
                    shengxl();
                    urll ="addStu";
                }
            });
        };

        //监听表单提交事件(添加功能的小窗口)
        form.on("submit(doSubmit)", function (data) {

         var spCA = {
             "stuId":$("#stuId").val(),
             "stuName":$("#stuNames").val(),
             "mobile":$("#mobiles").val(),
             "sex":$('input[name="sex"]:checked').val(),
             "birthday":$("#birthday").val(),
             "idType":$("#idTypes").val(),
             "idNum":$("#idNums").val(),
             "provincesId":$("select[name='provinceid']").val(),
             "citiesId":$("select[name='cityid']").val(),
             "areasId":$("select[name='areaid']").val()
                };


            console.log(spCA);
            // var sss = data.field[0]+ spCA;
            // console.log(sss);
            // 省下拉框
            $.ajax({
                type:"post",
                url:urll,
                contentType: "application/json;charset=utf-8",
                data:JSON.stringify(spCA),
                dataType:"json",
                success:function (result) {
                    layer.alert(result.message, {icon: 1});
                    //关闭窗口
                    layer.close(mainIndex);
                    //刷新数据表格
                    tableIns.reload();

                },error:function (result) {
                    layer.alert(result.message, {icon: 2});
                }
            });


            //
            // $.post(urll, data.field, function (result) {
            //     if (result.success) {
            //         layer.alert(result.message, {icon: 1});
            //         //关闭窗口
            //         layer.close(mainIndex);
            //         //刷新数据表格
            //         tableIns.reload();
            //     } else {
            //         layer.alert(result.message, {icon: 2});
            //     }
            // }, "json");
            return false;
        });


        //监听表格行工具栏事件(学生表)
        table.on("tool(studentTable)",function (obj) {
            switch (obj.event) {
                //编辑按钮
                case 'edit':
                    openUpdateWindow(obj.data);

                    break;

                //删除
                case 'delete':
                    deleteById(obj.data);
                    break;



            }
        });

        //打开编辑功能窗口
        function openUpdateWindow(data) {
            console.log(data);
            mainIndex =layer.open({
                type:1,//弹出层类型
                title:"修改账单",
                area: ['800px', '600px'],
                content:$("#addOrUpdateBillDiv"),//引用的窗口代码
                success:function () {
                    shengxlk(data);
                    //表单数据回显
                    form.val("dataFrm1",data);
                    //修改请求
                    urll = "updStu";
                }
            });
        };

        function shengxlk(data) {

            // 省下拉框
            $.ajax({
                type:"post",
                url:"provinces",
                contentType: "application/json;charset=utf-8",
                dataType:"json",
                success:function (plist) {
                    var phtml ="<option value=''>请选择1</option>";
                    for (let i = 0; i < plist.length; i++) {
                        phtml += "<option value="+plist[i].provinceid+">"+plist[i].province+"</option>";
                    }
                    layer.msg(phtml);
                    $("select[name='provinceid']").html(phtml);
                    $("select[name='provinceid']").val(data.provincesId);
                    layui.form.render('select'); //这个很重要,没有这个新的option不会显示
                    // layer.msg(data.)

                },error:function () {
                    layer.msg("添加请求失败1");
                }
            });

            //市下拉框
            $.ajax({
                type: "post",
                url:  "cities",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success: function (clist) {
                    layer.msg("1");
                    var chtml = "<option value=''>请选择1</option>";
                    for (let i = 0; i < clist.length; i++) {
                        if ($("select[name='provinceid']").val() == clist[i].provinceid) {
                            chtml += "<option value=" + clist[i].cityid + ">" + clist[i].city + "</option>";
                        }
                    }
                    $("select[name='cityid']").html(chtml);
                    $("select[name='cityid']").val(data.citiesId);
                    form.render('select'); //这个很重要,没有这个新的option不会显示
                    layer.msg(chtml);

                }, error: function () {
                    layer.msg("添加请求失败2");
                }
            });

            //区下拉框
            $.ajax({
                type: "post",
                url:  "areas",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success: function (alist) {
                    var ahtml = "<option value=''>请选择1</option>";
                    for (let i = 0; i < alist.length; i++) {
                        if ($("select[name='cityid']").val() == alist[i].cityid) {
                            ahtml += "<option value=" + alist[i].areaid + ">" + alist[i].area + "</option>";
                        }
                    }
                    $("select[name='areaid']").html(ahtml);
                    $("select[name='areaid']").val(data.areasId);
                    layer.msg(ahtml);
                    form.render('select'); //这个很重要,没有这个新的option不会显示
                }, error: function () {
                    layer.msg("添加请求失败3");
                }
            });

                //当select发生改变时
                form.on('select(provinceid)', function (dat) {
                    $("#cityid").empty();
                    $("#areaid").empty();

                    //市下拉框
                    $.ajax({
                        type: "post",
                        url:"cities",
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        success: function (clist) {
                            layer.msg("1");
                            var chtml = "<option value=''>请选择1</option>";
                            for (let i = 0; i < clist.length; i++) {
                                if ($("select[name='provinceid']").val() == clist[i].provinceid) {
                                    chtml += "<option value=" + clist[i].cityid + ">" + clist[i].city + "</option>";
                                }
                            }
                            $("select[name='cityid']").html(chtml);

                            form.render('select'); //这个很重要,没有这个新的option不会显示
                            layer.msg(chtml);

                        }, error: function () {
                            layer.msg("添加请求失败2");
                        }
                    });
                });

                //当select发生改变时
                form.on('select(cityid)', function (dat) {
                    $.ajax({
                        type: "post",
                        url: "areas",
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        success: function (alist) {
                            var ahtml = "<option value=''>请选择1</option>";
                            for (let i = 0; i < alist.length; i++) {
                                if ($("select[name='cityid']").val() == alist[i].cityid) {
                                    ahtml += "<option value=" + alist[i].areaid + ">" + alist[i].area + "</option>";
                                }
                            }
                            $("select[name='areaid']").html(ahtml);

                            layer.msg(ahtml);
                            form.render('select'); //这个很重要,没有这个新的option不会显示
                        }, error: function () {
                            layer.msg("添加请求失败3");
                        }
                    });


                });

        };


        function shengxl() {



        // 省下拉框
        $.ajax({
            type:"post",
            url:"provinces",
            contentType: "application/json;charset=utf-8",
            dataType:"json",
            success:function (plist) {
                var phtml ="<option value=''>请选择1</option>";
                for (let i = 0; i < plist.length; i++) {
                    phtml += "<option value="+plist[i].provinceid+">"+plist[i].province+"</option>";
                }
                layer.msg(phtml);
                $("select[name='provinceid']").html(phtml);
                // $("select[name='provinceid']").val(data.provincesId);
                layui.form.render('select'); //这个很重要,没有这个新的option不会显示
                // layer.msg(data.)

            },error:function () {
                layer.msg("添加请求失败1");
            }
        });

        //当select发生改变时
        form.on('select(provinceid)', function (dat) {
            //市下拉框
            $.ajax({
                type: "post",
                url:"cities",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success: function (clist) {
                    layer.msg("1");
                    var chtml = "<option value=''>请选择1</option>";
                    for (let i = 0; i < clist.length; i++) {
                        if ($("select[name='provinceid']").val() == clist[i].provinceid) {
                            chtml += "<option value=" + clist[i].cityid + ">" + clist[i].city + "</option>";
                        }
                    }
                    $("select[name='cityid']").html(chtml);
                    // $("select[name='cityid']").val(data.citiesId);
                    form.render('select'); //这个很重要,没有这个新的option不会显示
                    layer.msg(chtml);

                }, error: function () {
                    layer.msg("添加请求失败2");
                }
            });
        });

        //当select发生改变时
        form.on('select(cityid)', function (dat) {
            $.ajax({
                type: "post",
                url: "areas",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success: function (alist) {
                    var ahtml = "<option value=''>请选择1</option>";
                    for (let i = 0; i < alist.length; i++) {
                        if ($("select[name='cityid']").val() == alist[i].cityid) {
                            ahtml += "<option value=" + alist[i].areaid + ">" + alist[i].area + "</option>";
                        }
                    }
                    $("select[name='areaid']").html(ahtml);
                    // $("select[name='areaid']").val(data.areasId);
                    layer.msg(ahtml);
                    form.render('select'); //这个很重要,没有这个新的option不会显示
                }, error: function () {
                    layer.msg("添加请求失败3");
                }
            });


        });

    };


    /**
     * 删除账单
     */
    function deleteById(data) {
        console.log(data);
        console.log(data.stuId);
        //提示用户确认是否删除
        layer.confirm("确定要删除"+data.stuName+"学生吗？",{icon:3,title:"提示"},function (index) {
        //发送ajax请求

            $.ajax({
                type: "post",
                url:"deleteById",
                contentType: "application/json;charset=utf-8",
                data:JSON.stringify(data.stuId),
                dataType: "json",
                success: function (result) {
                    layer.alert(result.message,{icon:1});
                    //刷新数据表格
                    tableIns.reload();
                }, error: function (result) {
                    layer.alert(result.message,{icon:2});
                }
            });




        //     $.post(basePath+"/deleteById",{"stuId":data.stuId},function(result){
        //     // $.get("/student/areasId/",{"citiesid":val},function(result){
        //     if(result.success){
        //         layer.alert(result.message,{icon:1});
        //         //刷新数据表格
        //         tableIns.reload();
        //     }else{
        //         layer.alert(result.message,{icon:2});
        //     }
        // },"json");
        //关闭提示框
        layer.close(index);

    });
}


    /**
     * 批量删除
     */
    function batchDelete() {
        console.log("123");
        //获取表格对象
        var checkStatus = table.checkStatus('studentTable'); //billTable 即为基础参数 id 对应的值
        //判断是否有选中行
        if(checkStatus.data.length>0){
            //定义数组，保存选中行的ID
            var idArr = [];
            //循环遍历获取选中行(目的是获取选中的每一行的ID值)
            for (let i = 0; i < checkStatus.data.length; i++) {
                //将选中的ID值添加到数组的末尾
                idArr.push(checkStatus.data[i].stuId);
            }
            //将数组转成字符串
            // var ids = idArr.join(",");
            // console.log(ids);
            //提示用户是否删除
            layer.confirm("确定要删除这<fontcolor='red'>"+checkStatus.data.length+"</font>条数据吗？", {icon:3,title:"提示"},function (index) {
                //发送ajax请求
                $.ajax({
                    type: "post",
                    url:"batchDelete",
                    contentType: "application/json;charset=utf-8",
                    data:JSON.stringify({ids:idArr}),
                    dataType: "json",
                    success: function (result) {
                        layer.alert(result.message,{icon:1});
                        //刷新数据表格
                        tableIns.reload();
                    }, error: function (result) {
                        layer.alert(result.message,{icon:2});
                    }
                });


                // $.post(basePath+"/batchDelete",{"ids":ids},function(result){
                //     if(result.success){
                //         layer.alert(result.message,{icon:1});
                //         //刷新数据表格
                //         tableIns.reload();
                //     }else{
                //         layer.alert(result.message,{icon:2});
                //     }
                // },"json");

                //关闭提示框
                layer.close(index);
            });
        }else{
            layer.msg("请选择要删除的数据");
        }
    }




});


// $("#export").click(function () {
//     layui.layer.confirm('is not?', function(index){
//
//         // layer.confirm("确定导出吗?",function (index) {
//         location.href="export";
//         layui.layer.close(index);
//     })
// })

$("#import").click(function () {

    layui.layer.confirm("确定导入吗?",function () {
        location.href="import";
    })
})



/*
// 时间戳
function getYMDHMS(timestamp) {
    let time = new Date(timestamp)
    let year = time.getFullYear()
    const month = (time.getMonth() + 1).toString().padStart(2, '0')
    const date = (time.getDate()).toString().padStart(2, '0')
    const hours = (time.getHours()).toString().padStart(2, '0')
    const minute = (time.getMinutes()).toString().padStart(2, '0')
    const second = (time.getSeconds()).toString().padStart(2, '0')

    return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
}
*/



